import type { Router } from 'vue-router'

interface User {
    yhm: string
    xxzjbh: string
    rolemc: string
}

export interface MenuItem {
    path: string
    name?: string
    component?: string
    meta: {
        title: string
        icon: string
        type?: string
        hidden?: boolean
    }
    children?: MenuItem[]
}

export const getUser = (): User => {
    const userStr = localStorage.getItem('user')
    return userStr ? JSON.parse(userStr) : null
}

export const getMenu = (): MenuItem[] => {
    // 模拟菜单数据
    return [
        // {
        //     path: '/admin/dashboard',
        //     name: 'Dashboard',
        //     component: 'views/admin/dashboard/index',
        //     meta: {
        //         title: '控制台',
        //         icon: 'dashboard'
        //     }
        // },
        {
            path: '/admin/article',
            meta: {
                title: '文章管理',
                icon: 'document'
            },
            children: [
                {
                    path: '/admin/article/list',
                    meta: {
                        title: '文章列表',
                        icon: 'list'
                    }
                },
                // {
                //     path: '/admin/article/category',
                //     meta: {
                //         title: '分类管理',
                //         icon: 'folder'
                //     }
                // },
                // {
                //     path: '/admin/article/tag',
                //     meta: {
                //         title: '标签管理',
                //         icon: 'price-tag'
                //     }
                // }
            ]
        },
        {
            path: '/admin/comment',
            meta: {
                title: '评论管理',
                icon: 'chat-dot-round'
            }
        },
        {
            path: '/admin/moments',
            meta: {
                title: '我的说说',
                icon: 'chat-line-round'
            }
        },
        {
            path: '/admin/investment',
            meta: {
                title: '投资管理',
                icon: 'investment'
            },
            children: [
                {
                    path: '/admin/investment/summary',
                    meta: {
                        title: '投资小结',
                        icon: 'summary'
                    }
                },
                {
                    path: '/admin/investment/portfolio',
                    meta: {
                        title: '持仓管理',
                        icon: 'portfolio'
                    }
                }
            ]
        },
        // 访问日志
        {
            path: '/admin/access-log',
            meta: {
                title: '访问日志',
                icon: 'access-log'
            }
        }
        // {
        //     path: '/admin/user',
        //     meta: {
        //         title: '用户管理',
        //         icon: 'user'
        //     }
        // },
        // {
        //     path: '/admin/system',
        //     meta: {
        //         title: '系统设置',
        //         icon: 'setting'
        //     },
        //     children: [
        //         {
        //             path: '/admin/system/website',
        //             meta: {
        //                 title: '网站设置',
        //                 icon: 'monitor'
        //             }
        //         },
        //         {
        //             path: '/admin/system/backup',
        //             meta: {
        //                 title: '备份管理',
        //                 icon: 'box'
        //             }
        //         }
        //     ]
        // }
    ]
}

// 扩展 Router 类型
declare module 'vue-router' {
    interface Router {
        sc_getUser: () => User
        sc_getMenu: () => MenuItem[]
    }
}

// 扩展 router 实例方法
export const extendRouter = (router: Router) => {
    router.sc_getUser = getUser
    router.sc_getMenu = getMenu
}

// 清除所有认证信息
export function clearAuth() {
    removeToken()
    localStorage.removeItem('privateSpaceAuth')
    localStorage.removeItem('privateSpaceToken')
}

const TOKEN_KEY = 'token'

export function getToken(): string | null {
    return localStorage.getItem(TOKEN_KEY)
}

export function setToken(token: string): void {
    localStorage.setItem(TOKEN_KEY, token)
}

export function removeToken(): void {
    localStorage.removeItem(TOKEN_KEY)
} 